package co.edu.unal.satacredito.rest.util;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;

/**
 * 
 * @author jhon
 */
public class RestSettings {
	private static RestSettings INSTANCE = null;
	private static Client client;
	private static String REST_URL;

	private RestSettings() {
	}

	private synchronized static void createInstance() {
		if (INSTANCE == null) {
			INSTANCE = new RestSettings();
			createClient();
		}
	}

	private synchronized static void createClient() {

		Properties properties = new Properties();

		try {
			properties.load(new FileInputStream(System
					.getenv("SATACREDITO_PATH")
					+ "/"
					+ "config/connection"
					+ "/" + "administrator" + ".properties"));

			String consumer_key = properties.getProperty("REST_CONSUMER_KEY");
			String consumer_secret = properties
					.getProperty("REST_CONSUMER_SECRET");
			
			setREST_URL(properties.getProperty("REST_URL"));

			// create a new Jersey client
			client = Client.create();
			OAuthParameters parameters;
			parameters = new OAuthParameters().consumerKey(consumer_key)
					.signatureMethod("HMAC-SHA1").version("1.0").nonce();
			// create a new OAuth client filter passing the needed info
			OAuthClientFilter filter = new OAuthClientFilter(
					client.getProviders(), parameters,
					new OAuthSecrets().consumerSecret(consumer_secret), null,
					null, null, null);
			// Add filter to the client
			client.addFilter(filter);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public synchronized Client getClient() {
		return client;
	}

	public static RestSettings getInstance() {
		createInstance();
		return INSTANCE;
	}

	public static String getREST_URL() {
		return REST_URL;
	}

	public static void setREST_URL(String rEST_URL) {
		REST_URL = rEST_URL;
	}
}